import Vue from 'vue'
import store from '@/store'
import config from '@/config'

// 安卓环境，特殊：全局filter获取不到globalData，所以在外部定义
let _isAndroid = uni.getSystemInfoSync().platform === 'android'

/**
 * 按需调整图片分辨率，宽高可只传递一个
 *
 * 1、会判断资源是否支持可调整图片分辨率
 * 2、会判断是否是安卓环境，安卓环境自动使用webp
 * @param {Object} value
 * @param {Object} w 调整后的宽度
 * @param {Object} h 调整后的高度
 */
Vue.filter('resize', function(value, w, h) {
	if (!value) return ''
	if (value.indexOf('cdn.neigou.com') > -1 && (w || h)) {
		value += '?x-oss-process=image/resize,m_lfit'
		if (w) {
			value += ',w_' + w
		}
		if (h) {
			value += ',h_' + h
		}
		if(_isAndroid) {
		 value += ',image/format,webp'
		}
	}
	return value
})
// 去化
Vue.filter('removeMigu', value => {
  if (store.state.loginType == 'removeMigu') {
		return value.replace('*', '')
	} else {
		return value
	}
})


// 2020-01-01 10:50:36 ==》 2020.01.01
Vue.filter('formaTime', value => {
  let val = ''
  let reg = new RegExp('-', 'g')
  if (value) {
    val = value.substring(0, 10).replace(reg, '.')
  }
  return val
})
//使用范围 0-全部商品，1-指定商品，2-指定商品除外，3-指定分类，4-指定分类除外
Vue.filter('formatScopeValue', value => {
  let val = ''
  switch (value) {
    case 0:
      val = '全部商品'
      break
    case 1:
      val = '指定商品'
      break
    case 2:
      val = '指定商品除外'
      break
    case 3:
      val = '指定分类'
      break
    case 4:
      val = '指定分类除外'
      break
  }
  return val
})
//优惠券类型:1-满额减，2-满额折，3-满件减，-4满件折，5-立减
Vue.filter('formatCouponType', value => {
  let val = ''
  switch (value) {
    case 1:
      val = '满额减'
      break
    case 2:
      val = '满额折'
      break
    case 3:
      val = '满件减'
      break
    case 4:
      val = '满件折'
      break
    case 5:
      val = '立减'
      break
	case 6:
	  val = '每满额减'
	  break
  }
  return val
})

Vue.filter('tenThousand', function(number, str = 'w') {
  if (!number) return 0
  const numberInt = parseInt(number)
  return numberInt > 9999 ? parseInt(numberInt % 100000 / 10000) + str : numberInt
})

Vue.filter('numberFormat', function(number,str = 'w') {
  if (!number) return 0
  const numberInt = parseInt(number)
  return numberInt > 9999 ? parseInt(numberInt / 10000) + str : numberInt
})

Vue.filter('encryPhoneNumber', function(val)   {
  if(!val) return ''
  return val.substring(0,3)+'****'+val.substring(7)
})

Vue.filter('phoneForm', function(str) {
	if (!str) return ''
	const reg = /^1[3456789]\d{9}$/
	if (str.length == 11 && reg.test(str)) {
		const pat = /(\d{3})\d*(\d{4})/
		const phone = str.replace(pat,'$1****$2');
		return phone
	} else {
		return str
	}
})


Vue.filter('timeForm', function(time, str = 'YYYY.mm.dd') {
  if (!time) return ''
  if (time.toString().length == 10) time = time * 1000
  var date = new Date(time)
  return Vue.prototype.$utils.dateFormat(date, str)
})

Vue.filter('fixPrice', function(price, str = 2) {
  if (!price || typeof price != 'number') return '0.00'
  return (price / 100).toFixed(str)
})

Vue.filter('formatDateUgc', function(time) { //Ugc时间管道
	if (!time) return ''
	time = time.toString();
	let y = time.slice(0, 4);
	let m = time.slice(4, 6);
	let d = time.slice(6, 8);
	let h = time.slice(8, 10);
	let mm = time.slice(10, 12);
	var returnTime = y + "-" + m + "-" + d + "  " + h + ":" + mm;

	var sysDate = new Date();//当前时间
	let sYear = sysDate.getFullYear();
	let sMonth = sysDate.getMonth() + 1;
	let sDate = sysDate.getDate();
	let sHours = sysDate.getHours();
	let sMinutes = sysDate.getMinutes();

	if(sYear == y && sMonth == m && sDate == d){//年月日都相等的情况下只显示时间
	   returnTime = h + ":" + mm;
	   if(sHours == h){//时相等，判断
		   var timeDifference = sMinutes - mm;
		   if(timeDifference <= 10){
			  returnTime = "10分钟前";
		   }
		   if(timeDifference <= 5){
			  returnTime = "5分钟前";
		   }
		   if(timeDifference <= 1){
			  returnTime = "1秒前";
		   }
	   }
	}
	return returnTime;
})

//评价状态 :0-审核中，1-已发布，2-审核未通过，3已下线
Vue.filter('formatCommentStatus', value => {
  let val = ''
  switch (value) {
    case 0:
      val = '审核中'
      break
    case 1:
      val = '已发布'
      break
    case 2:
      val = '审核未通过'
      break
    case 3:
      val = '已下线'
      break
  }
  return val
})

Vue.filter('skuSpecForm', function(str) {//商品规格转换
	if (!str) return ''
	str = JSON.parse(str).join(';');
	return str
})

Vue.filter('priceForm', function(price, index) {//价格截取
	return Vue.prototype.$utils.priceConversion(price, index);
})
/**商品标题前端的便签
 * @param {Object} label
 */
Vue.filter('labelForm', function(label) {
	let key = ''
	if (label && Array.isArray(label)) {
		label.forEach(item => key += item)
		return key
	} else {
		return ''
	}
})
/**课程积分价格的显示
 * @param {Object} price 积分
 * @param {Object} type 整数还是小数
 */
Vue.filter('coursePriceForm', function(price, type) {
	const priceStr = (price / 100).toFixed(2) + "";
	if (priceStr.indexOf(".") !== -1) {
		const list = priceStr.split(".");
		if (list[1] == '00') list[1] = ''
		if (list[1].endsWith('0')) list[1] = list[1].slice(0, list[1].length -1)
		return type == "Int" ? list[0] : (list[1] ? "." + list[1] : '');
	} else {
		return type == "Int" ? priceStr : "";
	}
})

Vue.filter('secToTime', function(s){
	if (!s) return '00:00'
	const hour = fix0(Math.floor(s/3600))
	const min = fix0(Math.floor(s/60) % 60)
	const sec = fix0(s % 60)
	if (s > 3600) {
		return hour + ':' + min + ':' + sec
	} else if (s > 60) {
		return min + ':' + sec
	} else {
		return '00' + ':' + sec
	}

	function fix0(num) {
		return num < 10 ? '0' + num : '' + num
	}
})

Vue.filter('formatSkuSpec', function(v){
	if (!v) return ''
	const skuSpec = JSON.parse(v)
	let str = ''
	for (let k in skuSpec) {
		let sku = k + ':' + skuSpec[k]
		if (str) sku = ',' + sku
		str += sku
	}
	return str;
})
/**
 * 图片管道
 */
Vue.filter('imgUrlForm', function (v) {
	if (!v) return ''
	return `${config.imgUrlPath}${v}`
})
/**
 * 手机号344格式
 */
Vue.filter('phoneFormat', function (v) {
	if (!v) return ''
	let res = v.replace(/^(.{3})(.*)(.{4})$/,'$1 $2 $3')
	return res
})
